package ab.mes.service.impl;

import ab.mes.entity.Bin;
import ab.mes.entity.MaterialBinOut;
import ab.mes.mapper.BinMapper;
import ab.mes.service.BinService;
import ab.mes.tools.PageParams;
import ab.mes.tools.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BinServiceImpl implements BinService {
    @Autowired
    private BinMapper binMapper;

    //获取物料库位信息
    public Result getBin(){
        Result result = new Result();
        result.getMap().put("bin",binMapper.getBin());
        return result;
    }

    //根据库位编号查询物料库位信息
    public Result getBinById(String Id){
        Result result = new Result();
        result.getMap().put("bin",binMapper.getBinById(Id));
        return result;
    }

    //停启用物料库位
    public void updateStateById(String Id){
        Bin bin = (Bin)getBinById(Id).getMap().get("bin");
        bin.setBinState(!bin.getBinState());
        binMapper.updateStateById(Id,bin.getBinState());
    }

    //暂时没有实现
    //编辑物料库位
    public void updateById2(Bin bin){

    }

    //添加新库位
    public void insertBin(Bin bin){
        binMapper.insert(bin);
    }


    //物料出库管理
    public Result getMaterialBinOut(PageParams pageParams, String type){
        QueryWrapper<MaterialBinOut> wrapper = new QueryWrapper<>();
        wrapper.apply("orders.order_no = scheduling.order_Num");
        String s=wrapper.getCustomSqlSegment();
        Page<MaterialBinOut> page = new Page<MaterialBinOut>(pageParams.getPage(), pageParams.getRows());
        IPage<MaterialBinOut> aPage = binMapper.getMaterialBinOut(page,wrapper);//**
        pageParams.setTotal(aPage.getTotal());
        Result result = new Result();
        result.getMap().put("MaterialBinOut",aPage.getRecords());
        result.getMap().put("pageParams",pageParams);
        return result;
    }
}
